package issr.boundary.vaadin;

import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.ui.Panel;
import com.vaadin.ui.Tree;
import com.vaadin.ui.VerticalLayout;

public class Menu extends VerticalLayout implements ValueChangeListener {

    private Tree menuTree;
    private Panel panel;

    public Menu() {

        // Visual modifiers of the menu.

        setHeight("100%");
        setMargin(true);

        // Add a tree.
        menuTree = new Tree();
        menuTree.setMultiSelect(false);

        // Instantly receive value update events.
        menuTree.setImmediate(true);

        panel = new Panel("Sezioni");
        panel.addComponent(menuTree);
        panel.setHeight("100%");

        addComponent(panel);

        menuTree.addListener((ValueChangeListener) this);

    }

    /*
     * Populates the tree with hard coded data.
     * @param anyTree the tree we want to populate with data.
     */
    public void populate(final Object[][] actions) {

        for (int i = 0; i < actions.length; i++) {
            String temp = (String) (actions[i][0]);
            menuTree.addItem(temp);

            if (actions[i].length == 1) {
                menuTree.setChildrenAllowed(temp, false);

            } else {
                for (int j = 1; j < actions[i].length; j++) {
                    String temp1 = (String) (actions[i][j]);
                    menuTree.addItem(temp1);
                    menuTree.setParent(temp1, temp);
                    menuTree.setChildrenAllowed(temp1, false);
                }
            }
        }
        menuTree.expandItem(this);
    }

    public void populateMenu(final Object[] parents) {
        for (int i = 0; i < parents.length; i++) {
            String temp = (String) (parents[i]);
            menuTree.addItem(temp);
        }
    }

    public void populateChilds(final Object[] childs, Object parent) {
        for (int i = 0; i < childs.length; i++) {
            String temp = (String) (childs[i]);
            menuTree.addItem(temp);
            menuTree.setParent(temp, parent);
            menuTree.setChildrenAllowed(temp, false);
        }
        menuTree.expandItem(parent);
    }

    public void depopulate() {
        menuTree.removeAllItems();
    }

    /*
     *  Removes an item from menuTree.
     */
    public void removeCurrentTreeSelection() {

        if (!menuTree.isRoot(menuTree.getValue())) {

            Object id = menuTree.getParent(menuTree.getValue());
            Object id2 = menuTree.getValue();
            menuTree.select(id);
            menuTree.removeItem(id2);
        }
    }

    public Tree getTree() {
        return menuTree;
    }

    public void valueChange(ValueChangeEvent event) {
//        removeCurrentTreeSelection();
        if (menuTree.getValue() != null) {
            String clickedView = menuTree.getValue().toString();
            MainApplication.getProject().getHandler().switchView(clickedView);
        }
    }
}
